<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="运维,openvpn," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="前言自从公司配置了openvpn之后就可以在家里连到公司的内网，能访问部署在公司内网的项目。这让我对openvpn很感兴趣，于是我就开始查找相应的教程，并在寻找便宜的vps主机尝试搭建openvpn。我先后在华为云，腾讯云，搬瓦工上搭建了openvpn，不同的vps上搭建略有不同，但大体的原理是一样的。期间也遇到了不少坑，尝试搭建的过程大概花了一个多星期。在这里我想把搭建的过程以及遇到的问题纪录一">
<meta property="og:type" content="article">
<meta property="og:title" content="Centos7下搭建openvpn">
<meta property="og:url" content="http://jieminblog.tk/2017/03/07/Centos7下搭建openvpn/index.html">
<meta property="og:site_name" content="jiemin's Blog">
<meta property="og:description" content="前言自从公司配置了openvpn之后就可以在家里连到公司的内网，能访问部署在公司内网的项目。这让我对openvpn很感兴趣，于是我就开始查找相应的教程，并在寻找便宜的vps主机尝试搭建openvpn。我先后在华为云，腾讯云，搬瓦工上搭建了openvpn，不同的vps上搭建略有不同，但大体的原理是一样的。期间也遇到了不少坑，尝试搭建的过程大概花了一个多星期。在这里我想把搭建的过程以及遇到的问题纪录一">
<meta property="og:image" content="http://omdsjtsd2.bkt.clouddn.com/17-3-7/31012857-file_1488863440132_1800e.png">
<meta property="og:updated_time" content="2017-03-07T08:57:54.958Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Centos7下搭建openvpn">
<meta name="twitter:description" content="前言自从公司配置了openvpn之后就可以在家里连到公司的内网，能访问部署在公司内网的项目。这让我对openvpn很感兴趣，于是我就开始查找相应的教程，并在寻找便宜的vps主机尝试搭建openvpn。我先后在华为云，腾讯云，搬瓦工上搭建了openvpn，不同的vps上搭建略有不同，但大体的原理是一样的。期间也遇到了不少坑，尝试搭建的过程大概花了一个多星期。在这里我想把搭建的过程以及遇到的问题纪录一">
<meta name="twitter:image" content="http://omdsjtsd2.bkt.clouddn.com/17-3-7/31012857-file_1488863440132_1800e.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://jieminblog.tk/2017/03/07/Centos7下搭建openvpn/"/>





  <title> Centos7下搭建openvpn | jiemin's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">jiemin's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">本来无一物，何处惹尘埃</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://jieminblog.tk/2017/03/07/Centos7下搭建openvpn/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="我爱的乌托邦">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/touxiang.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="jiemin's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Centos7下搭建openvpn
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-03-07T05:48:42+08:00">
                2017-03-07
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>自从公司配置了openvpn之后就可以在家里连到公司的内网，能访问部署在公司内网的项目。这让我对openvpn很感兴趣，于是我就开始查找相应的教程，并在寻找便宜的vps主机尝试搭建openvpn。我先后在华为云，腾讯云，搬瓦工上搭建了openvpn，不同的vps上搭建略有不同，但大体的原理是一样的。期间也遇到了不少坑，尝试搭建的过程大概花了一个多星期。在这里我想把搭建的过程以及遇到的问题纪录一下。<br><a id="more"></a></p>
<h1 id="oepnvpn搭建"><a href="#oepnvpn搭建" class="headerlink" title="oepnvpn搭建"></a>oepnvpn搭建</h1><p>我使用过2种方式搭建过openvpn，一种是使用<strong>yum</strong>安装,另一种是通过<strong>docker-compose</strong>安装</p>
<h2 id="yum方式安装"><a href="#yum方式安装" class="headerlink" title="yum方式安装"></a>yum方式安装</h2><p>OpenVpn在默认的CentOS存储库中不可用。因此，我们需要安装Linux（EPEL）存储库。使用以下命令安装EPEL存储库</p>
<p><code>yum install epel-release</code></p>
<h4 id="1-安装openvpn"><a href="#1-安装openvpn" class="headerlink" title="1. 安装openvpn"></a>1. 安装openvpn</h4><p><code>yum install openvpn -y</code></p>
<h4 id="2-安装Easy-RSA"><a href="#2-安装Easy-RSA" class="headerlink" title="2. 安装Easy RSA"></a>2. 安装Easy RSA</h4><p><code>yum install easy-rsa -y</code></p>
<h4 id="3-配置openvpn"><a href="#3-配置openvpn" class="headerlink" title="3. 配置openvpn"></a>3. 配置openvpn</h4><p><code>cp /usr/share/doc/openvpn-*/sample/sample-config-files/server.conf  /etc/openvpn</code></p>
<p><code>vi /etc/openvpn/server.conf</code></p>
<p>在编辑server.conf文件时要注意以下几个地方</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">dh dh2048.pem</div><div class="line">push <span class="string">"redirect-gateway def1 bypass-dhcp"</span></div><div class="line">push <span class="string">"dhcp-option DNS 8.8.8.8"</span></div><div class="line">push <span class="string">"dhcp-option DNS 8.8.4.4"</span></div><div class="line">user nobody</div><div class="line">group nobody</div><div class="line">log openvpn.log</div><div class="line">;explicit-<span class="keyword">exit</span>-notify <span class="number">1</span></div></pre></td></tr></table></figure>
<p><code>log openvpn.log</code>这个配置可以看oepnvpn的启动和连接日志。我刚开始配置openvpn时没设置<strong>log</strong>，导致发生错误时不能准确找到错误原因，只能网上查错误的原因一个一个去尝试，效率非常低下。而有了<strong>log</strong>日志后出现问题就能比较准确定位了，比如<code>;explicit-exit-notify 1</code>，原始配置中是没有<code>;</code>的，启动openvpn时会报错,通过查看log日志能很快找到原因。个人觉得这个配置很重要，所以这里特别备注下。</p>
<h4 id="4-生成证书和秘钥"><a href="#4-生成证书和秘钥" class="headerlink" title="4. 生成证书和秘钥"></a>4. 生成证书和秘钥</h4><p><code>mkdir -p /etc/openvpn/easy-rsa/keys</code></p>
<p><code>cp -rf /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa</code></p>
<p><code>vi /etc/openvpn/easy-rsa/vars</code></p>
<p><code>vars</code>中需要修改以下内容,</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> KEY_COUNTRY=<span class="string">"CH"</span></div><div class="line"><span class="keyword">export</span> KEY_PROVINCE=<span class="string">"ZJ"</span></div><div class="line"><span class="keyword">export</span> KEY_CITY=<span class="string">"HZ"</span></div><div class="line"><span class="keyword">export</span> KEY_ORG=<span class="string">"DC"</span></div><div class="line"><span class="keyword">export</span> KEY_EMAIL=<span class="string">"example@gmail.com"</span></div><div class="line"><span class="keyword">export</span> KEY_OU=<span class="string">"Community"</span></div><div class="line"><span class="keyword">export</span> KEY_NAME=<span class="string">"server"</span></div><div class="line">. . .</div><div class="line"><span class="keyword">export</span> KEY_CN=openvpn.yourname.com</div></pre></td></tr></table></figure>
<p>由于版本不可检测，OpenSSL配置可能无法加载。为了避免这种情况，从openSSl文件名中删除版本号。</p>
<p><code>cp /etc/openvpn/easy-rsa/openssl-1.0.0.cnf /etc/openvpn/easy-rsa/openssl.cnf</code></p>
<p>接下来进行服务端证书生成</p>
<p><code>cd /etc/openvpn/easy-rsa</code></p>
<p><code>source ./vars</code></p>
<p><code>./clean-all</code></p>
<p><code>./build-ca</code></p>
<p><code>./build-key-server server</code></p>
<p><code>./build-dh</code></p>
<p><code>cd /etc/openvpn/easy-rsa/keys</code></p>
<p><code>cp dh2048.pem ca.crt server.crt server.key /etc/openvpn</code></p>
<p>客户端证书生成</p>
<p><code>cd /etc/openvpn/easy-rsa</code></p>
<p><code>./build-key client</code></p>
<h4 id="5-路由配置"><a href="#5-路由配置" class="headerlink" title="5. 路由配置"></a>5. 路由配置</h4><p>安装iptables</p>
<p><code>yum install iptables-services -y</code></p>
<p><code>systemctl mask firewalld</code></p>
<p><code>systemctl enable iptables</code></p>
<p><code>systemctl stop firewalld</code></p>
<p><code>systemctl start iptables</code></p>
<p><code>iptables --flush</code></p>
<p>配置路由转发</p>
<p><code>iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE</code></p>
<p><code>iptables-save &gt; /etc/sysconfig/iptables</code></p>
<p>开启路由转发</p>
<p><code>vi /etc/sysctl.conf</code></p>
<p>在<code>sysctl.conf</code>中增加<br><code>net.ipv4.ip_forward = 1</code></p>
<p>重启网络服务</p>
<p><code>systemctl restart network.service</code></p>
<p>这一步很重要，如果网络服务重启失败，请排查原因。这里报错的话一般和网卡设置有关,在<code>/etc/sysconfig/network-scripts</code>目录下查看网卡设置是否正确。参考<a href="http://blog.csdn.net/felixfeng/article/details/51852621" target="_blank" rel="external">centos7网卡配置</a></p>
<h4 id="6-启动openvpn"><a href="#6-启动openvpn" class="headerlink" title="6. 启动openvpn"></a>6. 启动openvpn</h4><p>设为开机启动<br><code>systemctl -f enable openvpn@server.service</code></p>
<p>启动openvpn</p>
<p><code>systemctl start openvpn@server.service</code></p>
<p>如果openvpn启动失败，请翻阅日志，日志默认在 <code>/etc/openvpn</code>路径下</p>
<p>至此服务端配置完毕。</p>
<h4 id="7-配置客户端"><a href="#7-配置客户端" class="headerlink" title="7. 配置客户端"></a>7. 配置客户端</h4><p>在服务端找到以下文件</p>
<p><code>/etc/openvpn/easy-rsa/keys/ca.crt</code></p>
<p><code>/etc/openvpn/easy-rsa/keys/client.crt</code></p>
<p><code>/etc/openvpn/easy-rsa/keys/client.key</code></p>
<p>把这些文件拷贝到客户端openvpn的config目录下</p>
<p>创建一个.ovpn配置文件，配置文件如下。</p>
<figure class="highlight lsl"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">client</div><div class="line">dev tun</div><div class="line">proto udp</div><div class="line">remote your_server_ip <span class="number">1194</span></div><div class="line">resolv-retry infinite</div><div class="line">nobind</div><div class="line">persist-<span class="type">key</span></div><div class="line">persist-tun</div><div class="line">comp-lzo</div><div class="line">verb <span class="number">3</span></div><div class="line">ca ca.crt</div><div class="line">cert client.crt</div><div class="line"><span class="type">key</span> client.<span class="type">key</span></div></pre></td></tr></table></figure>
<p>要注意的是客户端的.ovpn文件与服务端的server.conf某些配置上需要保持一致，否则会连接失败。.ovpn也放到 openvpn的config目录下。</p>
<p><strong>ca.crt,client.crt client.key与.opvn文件的集成:</strong></p>
<p>在openvpn文件中使用<code>&lt;ca&gt;&lt;/ca&gt;</code>,<code>&lt;cert&gt;&lt;/cert&gt;</code>,<code>&lt;key&gt;&lt;/key&gt;</code>标签，把对应的文件内容复制粘贴到标签当中去。</p>
<figure class="highlight asciidoc"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">client</div><div class="line">dev tun</div><div class="line">proto udp</div><div class="line">remote your<span class="emphasis">_server_</span>ip 1194</div><div class="line">resolv-retry infinite</div><div class="line">nobind</div><div class="line">persist-key</div><div class="line">persist-tun</div><div class="line">comp-lzo</div><div class="line">verb 3</div><div class="line">ca ca.crt</div><div class="line">cert client.crt</div><div class="line">key client.key</div><div class="line"></div><div class="line">&lt;ca&gt;</div><div class="line"><span class="code">......</span></div><div class="line">......</div><div class="line">&lt;/ca&gt;</div><div class="line"></div><div class="line">&lt;cert&gt;</div><div class="line">......</div><div class="line"><span class="code">......</span></div><div class="line">&lt;/cert&gt;</div><div class="line"></div><div class="line">&lt;key&gt;</div><div class="line">......</div><div class="line"><span class="code">......</span></div><div class="line">&lt;/key&gt;</div></pre></td></tr></table></figure>
<h2 id="docker-compose方式安装"><a href="#docker-compose方式安装" class="headerlink" title="docker-compose方式安装"></a>docker-compose方式安装</h2><h4 id="1-创建一个docker-compose-yml文件"><a href="#1-创建一个docker-compose-yml文件" class="headerlink" title="1.创建一个docker-compose.yml文件"></a>1.创建一个docker-compose.yml文件</h4><figure class="highlight dts"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="symbol">version:</span> <span class="string">'2'</span></div><div class="line"><span class="symbol">services:</span></div><div class="line"><span class="symbol">  openvpn:</span></div><div class="line"><span class="symbol">    cap_add:</span></div><div class="line">     - NET_ADMIN</div><div class="line"><span class="symbol">    image:</span> kylemanna/openvpn</div><div class="line"><span class="symbol">    container_name:</span> openvpn</div><div class="line"><span class="symbol">    ports:</span></div><div class="line">     - <span class="string">"1194:1194/udp"</span></div><div class="line"><span class="symbol">    restart:</span> always</div><div class="line"><span class="symbol">    volumes:</span></div><div class="line">     - .<span class="meta-keyword">/openvpn-data/</span>conf:<span class="meta-keyword">/etc/</span>openvpn</div></pre></td></tr></table></figure>
<h4 id="2-初始化配置文件和安装证书"><a href="#2-初始化配置文件和安装证书" class="headerlink" title="2.初始化配置文件和安装证书"></a>2.初始化配置文件和安装证书</h4><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">docker-compose <span class="keyword">run</span><span class="bash"> --rm openvpn ovpn_genconfig -u udp://VPN.SERVERNAME.COM</span></div><div class="line">docker-compose <span class="keyword">run</span><span class="bash"> --rm openvpn ovpn_initpki</span></div></pre></td></tr></table></figure>
<h4 id="3-启动openvpn"><a href="#3-启动openvpn" class="headerlink" title="3. 启动openvpn"></a>3. 启动openvpn</h4><p><code>docker-compose up -d openvpn</code></p>
<h4 id="4-查看启动日志"><a href="#4-查看启动日志" class="headerlink" title="4. 查看启动日志"></a>4. 查看启动日志</h4><p><code>docker-compose logs -f</code></p>
<h4 id="5-生成客户端证书"><a href="#5-生成客户端证书" class="headerlink" title="5.生成客户端证书"></a>5.生成客户端证书</h4><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">export CLIENTNAME=<span class="string">"your_client_name"</span></div><div class="line"></div><div class="line">docker-compose <span class="keyword">run</span><span class="bash"> --rm openvpn easyrsa build-client-full <span class="variable">$CLIENTNAME</span></span></div></pre></td></tr></table></figure>
<h4 id="6-生成集成客户端-ovpn文件"><a href="#6-生成集成客户端-ovpn文件" class="headerlink" title="6. 生成集成客户端.ovpn文件"></a>6. 生成集成客户端.ovpn文件</h4><p><code>docker-compose run --rm openvpn ovpn_getclient $CLIENTNAME &gt; $CLIENTNAME.ovpn</code></p>
<h2 id="搭建openvpn过程中遇到的那些坑"><a href="#搭建openvpn过程中遇到的那些坑" class="headerlink" title="搭建openvpn过程中遇到的那些坑"></a>搭建openvpn过程中遇到的那些坑</h2><h4 id="1-华为云上搭建好了openvpn，客户端一直连接不上。"><a href="#1-华为云上搭建好了openvpn，客户端一直连接不上。" class="headerlink" title="1.华为云上搭建好了openvpn，客户端一直连接不上。"></a>1.华为云上搭建好了openvpn，客户端一直连接不上。</h4><p><strong>原因:</strong> 华为云中有个安全策略设置，安全策略中1194端口没有开放，需要开放后才能连接。安全策略设置在华为云后台控制中设置</p>
<h4 id="2-搬瓦工vps中安装不了docker。"><a href="#2-搬瓦工vps中安装不了docker。" class="headerlink" title="2. 搬瓦工vps中安装不了docker。"></a>2. 搬瓦工vps中安装不了docker。</h4><p><strong>原因:</strong><br>使用docker-compose方式安装openvpn需要先安装docker。搬瓦工采用openvz架构搭建，其linux内核版本为<code>2.6.32-042stab120.18</code>,不可升级，而docker需要linux内核3.1以上，所以搬瓦工上不能安装docker,也不能使用docker-compose的方式安装openvpn。</p>
<h4 id="3-搬瓦工上openvpn连接上了，但不能上网。"><a href="#3-搬瓦工上openvpn连接上了，但不能上网。" class="headerlink" title="3. 搬瓦工上openvpn连接上了，但不能上网。"></a>3. 搬瓦工上openvpn连接上了，但不能上网。</h4><p><strong>原因:</strong> 转发失败，由于搬瓦工上一个网卡上有两个ip地址所致,默认转发到第一个ip地址上。</p>
<p><img src="http://omdsjtsd2.bkt.clouddn.com/17-3-7/31012857-file_1488863440132_1800e.png" alt=""></p>
<p><strong>解决办法:</strong> </p>
<p>编辑转发配置文件</p>
<p><code>vi /etc/sysconfig/iptables</code></p>
<p>删除原来的转发规则</p>
<p><code>-A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE</code></p>
<p>新增转发规则</p>
<p><code>-A POSTROUTING -s 10.8.0.0/24 -o venet0 -j SNAT --to-source your ip</code></p>
<p>保存后重启网络即可</p>
<p><code>systemctl restart network</code></p>
<h2 id="结束语"><a href="#结束语" class="headerlink" title="结束语"></a>结束语</h2><p>openvpn搭建只要弄懂了它的实现原理，按照步骤一步步搭建，还是很容易成功。遇到问题先看日志，准确定位问题发生的环节，就能针对性解决问题。我在搭建过程中一般出现问题就是在转发规则配置的地方，不同的vps在这上面配置可能存在着差异，需要根据vps的实际情况进行配置。搭建openvpn中对于centos7中的网络配置这块内容也了解了不少，算是收获良多，搭建成功以后也是满满的成就感！在此把搭建过程和遇到过的问题纪录了下，方便以后查阅。</p>
<p><strong>参考资料</strong></p>
<p> <a href="https://github.com/kylemanna/docker-openvpn/blob/master/docs/docker-compose.md" target="_blank" rel="external">docker-compose方式安装openvpn</a></p>
<p> <a href="http://www.tuicool.com/articles/V3aIF3v" target="_blank" rel="external">centos7安装openvpn</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/运维/" rel="tag"># 运维</a>
          
            <a href="/tags/openvpn/" rel="tag"># openvpn</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/03/06/Markdown语法/" rel="next" title="Markdown语法">
                <i class="fa fa-chevron-left"></i> Markdown语法
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/03/07/Nginx反向代理小应用/" rel="prev" title="Nginx反向代理小应用">
                Nginx反向代理小应用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/touxiang.jpeg"
               alt="我爱的乌托邦" />
          <p class="site-author-name" itemprop="name">我爱的乌托邦</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">17</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#oepnvpn搭建"><span class="nav-number">2.</span> <span class="nav-text">oepnvpn搭建</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#yum方式安装"><span class="nav-number">2.1.</span> <span class="nav-text">yum方式安装</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-安装openvpn"><span class="nav-number">2.1.0.1.</span> <span class="nav-text">1. 安装openvpn</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-安装Easy-RSA"><span class="nav-number">2.1.0.2.</span> <span class="nav-text">2. 安装Easy RSA</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-配置openvpn"><span class="nav-number">2.1.0.3.</span> <span class="nav-text">3. 配置openvpn</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-生成证书和秘钥"><span class="nav-number">2.1.0.4.</span> <span class="nav-text">4. 生成证书和秘钥</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-路由配置"><span class="nav-number">2.1.0.5.</span> <span class="nav-text">5. 路由配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-启动openvpn"><span class="nav-number">2.1.0.6.</span> <span class="nav-text">6. 启动openvpn</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#7-配置客户端"><span class="nav-number">2.1.0.7.</span> <span class="nav-text">7. 配置客户端</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#docker-compose方式安装"><span class="nav-number">2.2.</span> <span class="nav-text">docker-compose方式安装</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-创建一个docker-compose-yml文件"><span class="nav-number">2.2.0.1.</span> <span class="nav-text">1.创建一个docker-compose.yml文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-初始化配置文件和安装证书"><span class="nav-number">2.2.0.2.</span> <span class="nav-text">2.初始化配置文件和安装证书</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-启动openvpn"><span class="nav-number">2.2.0.3.</span> <span class="nav-text">3. 启动openvpn</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-查看启动日志"><span class="nav-number">2.2.0.4.</span> <span class="nav-text">4. 查看启动日志</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-生成客户端证书"><span class="nav-number">2.2.0.5.</span> <span class="nav-text">5.生成客户端证书</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-生成集成客户端-ovpn文件"><span class="nav-number">2.2.0.6.</span> <span class="nav-text">6. 生成集成客户端.ovpn文件</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#搭建openvpn过程中遇到的那些坑"><span class="nav-number">2.3.</span> <span class="nav-text">搭建openvpn过程中遇到的那些坑</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-华为云上搭建好了openvpn，客户端一直连接不上。"><span class="nav-number">2.3.0.1.</span> <span class="nav-text">1.华为云上搭建好了openvpn，客户端一直连接不上。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-搬瓦工vps中安装不了docker。"><span class="nav-number">2.3.0.2.</span> <span class="nav-text">2. 搬瓦工vps中安装不了docker。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-搬瓦工上openvpn连接上了，但不能上网。"><span class="nav-number">2.3.0.3.</span> <span class="nav-text">3. 搬瓦工上openvpn连接上了，但不能上网。</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结束语"><span class="nav-number">2.4.</span> <span class="nav-text">结束语</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">我爱的乌托邦</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  



  
  

  

  

  

  


  

</body>
</html>
